package retention_person

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/retention_person"
	retention_personReq "github.com/flipped-aurora/gin-vue-admin/server/model/retention_person/request"
	"gorm.io/gorm"
)

type CasRetentionPersonsService struct{}

// CreateCasRetentionPersons 创建casRetentionPersons表记录
// Author [yourname](https://github.com/yourname)
func (casRetentionPersonsService *CasRetentionPersonsService) CreateCasRetentionPersons(casRetentionPersons *retention_person.CasRetentionPersons) (err error) {
	casRetentionPersons.FullColumns()
	err = global.GVA_DB.Create(casRetentionPersons).Error
	return err
}

// DeleteCasRetentionPersons 删除casRetentionPersons表记录
// Author [yourname](https://github.com/yourname)
func (casRetentionPersonsService *CasRetentionPersonsService) DeleteCasRetentionPersons(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&retention_person.CasRetentionPersons{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&retention_person.CasRetentionPersons{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteCasRetentionPersonsByIds 批量删除casRetentionPersons表记录
// Author [yourname](https://github.com/yourname)
func (casRetentionPersonsService *CasRetentionPersonsService) DeleteCasRetentionPersonsByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&retention_person.CasRetentionPersons{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&retention_person.CasRetentionPersons{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateCasRetentionPersons 更新casRetentionPersons表记录
// Author [yourname](https://github.com/yourname)
func (casRetentionPersonsService *CasRetentionPersonsService) UpdateCasRetentionPersons(casRetentionPersons retention_person.CasRetentionPersons) (err error) {
	casRetentionPersons.FullColumns()
	err = global.GVA_DB.Model(&retention_person.CasRetentionPersons{}).Where("id = ?", casRetentionPersons.ID).Updates(&casRetentionPersons).Error
	return err
}

// GetCasRetentionPersons 根据ID获取casRetentionPersons表记录
// Author [yourname](https://github.com/yourname)
func (casRetentionPersonsService *CasRetentionPersonsService) GetCasRetentionPersons(ID string) (casRetentionPersons retention_person.CasRetentionPersons, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&casRetentionPersons).Error
	return
}

// GetCasRetentionPersonsInfoList 分页获取casRetentionPersons表记录
// Author [yourname](https://github.com/yourname)
func (casRetentionPersonsService *CasRetentionPersonsService) GetCasRetentionPersonsInfoList(info retention_personReq.CasRetentionPersonsSearch) (list []retention_person.CasRetentionPersons, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&retention_person.CasRetentionPersons{})
	var casRetentionPersonss []retention_person.CasRetentionPersons
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.State != nil && *info.State != "" {
		db = db.Where("state = ?", *info.State)
	}

	if info.Name != nil && *info.Name != "" {
		db = db.Where("name LIKE ?", "%"+*info.Name+"%")
	}
	if info.IdNumber != nil && *info.IdNumber != "" {
		db = db.Where("id_number = ?", *info.IdNumber)
	}
	if info.Retention != nil && *info.Retention != "" {
		db = db.Where("retention = ?", *info.Retention)
	}
	if info.TownCode != nil && *info.TownCode != "" {
		db = db.Where("town_code = ?", *info.TownCode)
	}
	if info.LiveTownCode != nil && *info.LiveTownCode != "" {
		db = db.Where("live_town_code = ?", *info.LiveTownCode)
	}
	if info.ContactName != nil && *info.ContactName != "" {
		db = db.Where("contact_name = ?", *info.ContactName)
	}
	if info.StartActivateAt != nil && info.EndActivateAt != nil {
		db = db.Where("activate_at BETWEEN ? AND ? ", info.StartActivateAt, info.EndActivateAt)
	}
	if info.StartSubmitAt != nil && info.EndSubmitAt != nil {
		db = db.Where("submit_at BETWEEN ? AND ? ", info.StartSubmitAt, info.EndSubmitAt)
	}
	if info.StartReportAt != nil && info.EndReportAt != nil {
		db = db.Where("report_at BETWEEN ? AND ? ", info.StartReportAt, info.EndReportAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if info.Sort != "" {
		if info.Order == "desc" {
			db = db.Order(info.Sort + " desc")
		} else {
			db = db.Order(info.Sort)
		}
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&casRetentionPersonss).Error
	return casRetentionPersonss, total, err
}
func (casRetentionPersonsService *CasRetentionPersonsService) GetCasRetentionPersonsPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
